package cn.kgc.crowdfundingadmin03.controller;


import cn.kgc.crowdfundingadmin03.common.ResultEntity;
import cn.kgc.crowdfundingadmin03.pojo.Tag;
import cn.kgc.crowdfundingadmin03.service.TagService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-03-02 8:26
 */
@RestController
public class TagController {

    @Resource
    TagService tagService;

    @RequestMapping("/tag/get/whole/tree")
    public ResultEntity<Tag> getWholeTree() {
        //查询所有的树节点用于组装
        List<Tag> all = tagService.getAll();
        //转成map格式
        Map<Integer, Tag> map = new HashMap<>();
        for (Tag tag : all) {
            map.put(tag.getId(), tag);
        }

        //声明变量存储 跟节点对象
        Tag rootNode = null;

        for (Tag tag : all) {
            //父节点 如果父节点为null则
            if (tag.getPid() == null) {
                rootNode = tag;
                continue;
            }
            //是子节点从all找到父节点,并添加到父节点的子节点集合
            //get  //获取指定键所映射的值
            map.get(tag.getPid()).getChildren().add(tag);
        }
        return ResultEntity.successWithoutData(rootNode);
    }

}
